package ml;

import java.util.ArrayList;

import util.DataUtil;
import util.MinMaxIndexAndValue;
public class LabelMaker {
	private int m_time_interval;
	private double m_min_profit;
	private double m_max_loss;

	public LabelMaker(int time_interval, double min_profit, double max_loss) {
		m_time_interval = time_interval;
		m_min_profit = min_profit;
		m_max_loss = max_loss;
	}

	// TODO: Change the method of calculating the rise and drop labels.
	public ArrayList<Boolean> calculate(ArrayList<Double> data, int begin, int label_num,
					boolean less_is_pos) {
		if ((label_num + begin + m_time_interval) > data.size()) {
			return null;
		}

		ArrayList<Boolean> result = new ArrayList<Boolean>(label_num);

		MinMaxIndexAndValue min_max = new MinMaxIndexAndValue(begin - 1, begin - 1, 0, 0);

		double threshold = 0.5;
		if (less_is_pos) {
			threshold = -0.5;
		}

		int end = begin + label_num;
		double current_data = 0;
		for (int i = begin; i < end; ++i) {
			current_data = data.get(i);

			if ((min_max.index_min < i) || (min_max.index_max < i)) {
				// need completely calculating
				min_max = DataUtil.FindMinMax(data, i, i + m_time_interval);
			} else {
				// can be accelerated
				if (current_data < min_max.min) {
					min_max.min = current_data;
					min_max.index_min = i;
				}
				if (current_data > min_max.max) {
					min_max.max = current_data;
					min_max.index_max = i;
				}
			}

			int label = calculateLabel(current_data, min_max.min, min_max.max);
			if (label < threshold) {
				result.add(less_is_pos);
			} else {
				result.add(!less_is_pos);
			}
		}

		return result;
	}

	private int calculateLabel(double current, double min, double max) {
		int ret = 0;

		if ((max - current) > m_min_profit) {
			if ((current - min) < m_max_loss) {
				// a large rise
				ret = 1;
			}
		} else if ((current - min) > m_min_profit) {
			if ((max - current) < m_max_loss) {
				// a large drop
				ret = -1;
			}
		}

		return ret;
	}
}
